﻿
//Author: 张亚涛
//createtime:2015-12-15 23:08:47
//Description:

using System;
using System.Data;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using NS.DAL.Systems;
using Model;
using DAL;

namespace DAL
{
    /// <summary>
    /// 该类提供了一系列操作 "Member" 表的方法;
    /// </summary>
    public partial class MemberBase
    {
        const string SQL_Select = "select [MemberID],[OpenID],[Mobile],[TrueName],[NickName],[Sex],[Age],[City],[HeadImgUrl],[PayWord],[IsLock],[CardNumber] from [Member] ";
        #region .对本表的维护.
        #region select data
        /// <summary>
        /// 返回Member表的所有数据 
        /// </summary>
        /// <returns></returns>        
        public static List<Member> GetAllMember()
        {
            List<Member> memberList = new List<Member>();
            SqlDataReader reader = null;

            string sql = SQL_Select;
            reader = SQLHelper.ExecuteDataReader(sql, null);
            while (reader.Read())
            {
                Member member = new Member(reader);
                memberList.Add(member);
            }
            reader.Close();
            return memberList;
        }
        /// <summary>
        /// 根据Member表的MemberID字段返回数据 
        /// </summary>
        /// <param name="memberID">MemberID</param>
        /// <returns></returns>       
        public static Member GetMemberByMemberID(long memberID)
        {
            Member member = null;
            SqlDataReader reader = null;

            string sql = SQL_Select + "where [MemberID] = @MemberID";
            SqlParameter[] paras = new SqlParameter[]{
            new SqlParameter("@MemberID",memberID)
            };
            reader = SQLHelper.ExecuteDataReader(sql, paras);
            if (reader.Read())
            {
                member = new Member(reader);
            }
            reader.Close();
            return member;
        }
        #endregion
        #region delete data
        /// <summary>
        /// 根据Member表的MemberID字段删除数据 
        /// </summary>
        /// <param name="memberID">memberID</param>
        /// <returns></returns>        
        public static int DeleteMemberByMemberID(long memberID)
        {
            string sql = "delete from [Member] where [MemberID] = @MemberID";
            SqlParameter[] paras = new SqlParameter[]{
            new SqlParameter("@MemberID",memberID)
            };
            return SQLHelper.ExecuteNonQuery(sql, paras);
        }
        #endregion
        #region update data
        /// <summary>
        /// prepare parameters 
        /// </summary>
        public static SqlParameter[] PrepareCommandParameters(Member member)
        {
            SqlParameter[] paras = new SqlParameter[]{
            new SqlParameter("@MemberID",member.MemberID),
            new SqlParameter("@OpenID",member.OpenID),
            new SqlParameter("@Mobile",member.Mobile),
            new SqlParameter("@TrueName",member.TrueName),
            new SqlParameter("@NickName",member.NickName),
            new SqlParameter("@Sex",member.Sex),
            new SqlParameter("@Age",member.Age),
            new SqlParameter("@City",member.City),
            new SqlParameter("@HeadImgUrl",member.HeadImgUrl),
            new SqlParameter("@PayWord",member.PayWord),
            new SqlParameter("@IsLock",member.IsLock),
            new SqlParameter("@CardNumber",member.CardNumber)    
            };
            return paras;
        }
        /// <summary>
        /// 根据Member表的MemberID字段更新数据 
        /// </summary> 
        /// <param name="Member">member</param>
        /// <returns></returns>       
        public static int UpdateMemberByMemberID(Member member)
        {
            string sql = "update [Member] set [OpenID] = @OpenID,[Mobile] = @Mobile,[TrueName] = @TrueName,[NickName] = @NickName,[Sex] = @Sex,[Age] = @Age,[City] = @City,[HeadImgUrl] = @HeadImgUrl,[PayWord] = @PayWord,[IsLock] = @IsLock,[CardNumber] = @CardNumber where [MemberID] = @MemberID";
            SqlParameter[] paras = PrepareCommandParameters(member);
            return SQLHelper.ExecuteNonQuery(sql, paras);
        }
        #endregion
        #region insert data
        /// <summary>
        /// 向Member表插入一条数据，返回自增列数值，插入不成功则返回-1
        /// </summary>
        /// <param name="member">Member</param>       
        /// <returns></returns>        
        public static int AddMember(Member member)
        {
            string sql = "insert into [Member]([OpenID],[Mobile],[TrueName],[NickName],[Sex],[City],[HeadImgUrl],[PayWord],[IsLock],[CardNumber])values(@OpenID,@Mobile,@TrueName,@NickName,@Sex,@City,@HeadImgUrl,@PayWord,@IsLock,@CardNumber)    select @@identity";
            SqlParameter[] paras = PrepareCommandParameters(member);
            int id = -1;

            string _ID = SQLHelper.ExecuteScalar(sql, paras).ToString();
            int.TryParse(_ID, out id);

            return id;
        }
        #endregion
        #endregion

        #region .SQL语句汇总.
        /// <summary>
        /// 向Member表插入一条数据（SQL语句）
        /// </summary>
        /// <param name="m">Member</param>       
        /// <returns></returns>        
        public static string AddMember_SQL(Member m)
        {
            string sql = "insert into [Member]([OpenID],[Mobile],[TrueName],[NickName],[Sex],[City],[HeadImgUrl],[PayWord],[IsLock],[CardNumber]) ";
            sql += " values ('" + m.OpenID + "','" + m.Mobile + "','" + m.TrueName + "','" + m.NickName + "','" + m.Sex + "','" + m.City + "','" + m.HeadImgUrl + "','" + m.PayWord + "','" + m.IsLock + "','" + m.CardNumber + "');";
            return sql;
        }

        /// <summary>
        /// 返回Member表的所有数据 （SQL语句）
        /// </summary>
        /// <returns></returns>        
        public static string GetAllMember_SQL()
        {
            return SQL_Select;
        }
        #endregion
    }
}